using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectCodeS.Models;

namespace ProjectCodeS.Controllers
{   
    public class TicketItemsController : Controller
    {
		private readonly ITicketRepository ticketRepository;
		private readonly IItemRepository itemRepository;
		private readonly ITicketItemRepository ticketitemRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public TicketItemsController() : this(new TicketRepository(), new ItemRepository(), new TicketItemRepository())
        {
        }

        public TicketItemsController(ITicketRepository ticketRepository, IItemRepository itemRepository, ITicketItemRepository ticketitemRepository)
        {
			this.ticketRepository = ticketRepository;
			this.itemRepository = itemRepository;
			this.ticketitemRepository = ticketitemRepository;
        }

        //
        // GET: /TicketItems/

        public ViewResult Index()
        {
            return View(ticketitemRepository.AllIncluding(ticketitem => ticketitem.Ticket, ticketitem => ticketitem.Item, ticketitem => ticketitem.Colors, ticketitem => ticketitem.Textures));
        }

        //
        // GET: /TicketItems/Details/5

        public ViewResult Details(int id)
        {
            return View(ticketitemRepository.Find(id));
        }

        //
        // GET: /TicketItems/Create

        public ActionResult Create()
        {
			ViewBag.PossibleTickets = ticketRepository.All;
			ViewBag.PossibleItems = itemRepository.All;
            return View();
        } 

        //
        // POST: /TicketItems/Create

        [HttpPost]
        public ActionResult Create(TicketItem ticketitem)
        {
            if (ModelState.IsValid) {
                ticketitemRepository.InsertOrUpdate(ticketitem);
                ticketitemRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleTickets = ticketRepository.All;
				ViewBag.PossibleItems = itemRepository.All;
				return View();
			}
        }
        
        //
        // GET: /TicketItems/Edit/5
 
        public ActionResult Edit(int id)
        {
			ViewBag.PossibleTickets = ticketRepository.All;
			ViewBag.PossibleItems = itemRepository.All;
             return View(ticketitemRepository.Find(id));
        }

        //
        // POST: /TicketItems/Edit/5

        [HttpPost]
        public ActionResult Edit(TicketItem ticketitem)
        {
            if (ModelState.IsValid) {
                ticketitemRepository.InsertOrUpdate(ticketitem);
                ticketitemRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleTickets = ticketRepository.All;
				ViewBag.PossibleItems = itemRepository.All;
				return View();
			}
        }

        //
        // GET: /TicketItems/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(ticketitemRepository.Find(id));
        }

        //
        // POST: /TicketItems/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ticketitemRepository.Delete(id);
            ticketitemRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                ticketRepository.Dispose();
                itemRepository.Dispose();
                ticketitemRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

